package cn.itcast.utils;

import cn.hutool.crypto.Mode;
import cn.hutool.crypto.Padding;
import cn.hutool.crypto.digest.DigestUtil;
import cn.hutool.crypto.symmetric.AES;
import cn.hutool.extra.mail.MailUtil;
import cn.hutool.json.JSONObject;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;

public class AESUtils {
    // key：AES模式下，key必须为16位
    public static final String KEY="BBSWA166442B66F0";
    // iv：偏移量，ECB模式不需要，CBC模式下必须为16位
    public static final String IV="b8a4c89cdc776729";

    /**
     * 加密
     * @param str
     * @return
     */
    public static String encrypt(String str){
        // AES aes = new AES(Mode.ECB, Padding.PKCS5Padding, key.getBytes());
        AES aes = new AES(Mode.CBC, Padding.PKCS5Padding, KEY.getBytes(), IV.getBytes());
        // 加密并进行Base转码
        String encrypt = aes.encryptBase64(str);
        String temp=aes.encryptBase64(str,"utf-8");
        return encrypt;
    }
    public static String deciphering(String str){
        AES aes = new AES(Mode.CBC, Padding.PKCS5Padding, KEY.getBytes(), IV.getBytes());
        // 加密并进行Base转码
        byte[] decryptDataBase64 = aes.decrypt(str);
        return new String(decryptDataBase64, StandardCharsets.UTF_8);
    }

    public static void main(String[] args) throws UnsupportedEncodingException {
        JSONObject jsonObject=new JSONObject();
        jsonObject.putOnce("id",3015058);
//        jsonObject.putOnce("limit",10);
//        jsonObject.putOnce("collection_id",469);
//        jsonObject.putOnce("type_field",2);
//        jsonObject.putOnce("phone_number","13803993919");
//        jsonObject.putOnce("token","");
//        jsonObject.putOnce("sign_time",1680750417);
        String str=jsonObject.toString();
        String encry=encrypt(str);
        System.out.println(encry);
//        System.out.println(deciphering(encry));
//       String str=deciphering("7GScSOMowT2R|41Q;CSKKg~~");
        //id="+detailDTO.getId()+"&key=woshijiamijiekou"+"&time="+time
//       System.out.println(str);
        String demo="id=13445&time=1681561547601&key=woshijiamijiekou";
        System.out.println(DigestUtil.md5Hex(demo));
        MailUtil.send("1027060531@qq.com", "文本邮件测试111", "IT技术分享社区，一个有态度的互联网分享平台！", false);


    }
}
